package com.zut.hospital.service.diagnose;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.dao.diagnose.PatientMapper;
import com.zut.hospital.dao.diagnose.RecordMapper;
import com.zut.hospital.dao.staff.OfficeMapper;
import com.zut.hospital.dao.staff.UserMapper;
import com.zut.hospital.entity.diagnose.Patient;
import com.zut.hospital.entity.diagnose.PatientExample;
import com.zut.hospital.entity.diagnose.Record;
import com.zut.hospital.entity.diagnose.RecordExample;
import com.zut.hospital.entity.staff.Office;
import com.zut.hospital.entity.staff.OfficeExample;
import com.zut.hospital.entity.staff.User;
import com.zut.hospital.entity.staff.UserExample;
import com.zut.hospital.service.examination.PhyexamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 侯捷
 * @package_name com.zut.hospital.service.diagnose
 * @date 2019/6/10 22:39
 * @description
 */
@Service
public class RecordSreviceImpl implements RecordService{
    @Autowired
    RecordMapper recordMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    PatientMapper patientMapper;
    @Autowired
    OfficeMapper officeMapper;
    @Autowired
    RecordService recordService;
    @Autowired
    PhyexamService phyexamService;
    @Autowired
    PatientService patientService;

    @Override
    public String layuiJsonParse(List<Map<String, String>> list,int size) {
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 0);
        layuiMap.put("msg", "success");
        layuiMap.put("count", size);
        layuiMap.put("data", list);
        return JSON.toJSONString(layuiMap);
    }

    @Override
    public int insertOneRecord(Record record){
        return recordMapper.insertSelective(record);
    }

    @Override
    public String getUserInfoByName(String name) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andAdNameEqualTo(name);
       List<User> list= userMapper.selectByExample(userExample);
       String id=list.get(0).getOfcId().toString();
       String power=list.get(0).getAdPower().toString();
        Map<String,String> map;
        map=new HashMap<>();
        map.put("ofcId",id);
        map.put("power",power);
        System.out.println(JSON.toJSONString(map));
        return JSON.toJSONString(map);
    }

    @Override
    public int getOfcIdBySession(String name){
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andAdNameEqualTo(name);
        List<User> list= userMapper.selectByExample(userExample);
        int id=list.get(0).getOfcId();
        System.out.println(id+"***id****");
        return  id;
    }
    @Override
    public String getInfoBySession(Integer ofcId) {
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatOfcIdEqualTo(ofcId);
        List<Patient> list=patientMapper.selectByExample(patientExample);

        Map<String,String> patientMap;
        List<Map<String,String>> petientList=new ArrayList<>();
        for (Patient patient : list) {
            patientMap=new HashMap<>();
            patientMap.put("id",patient.getId().toString());
            patientMap.put("patName",patient.getPatName());
            if(patient.getPatSex()==1)
                patientMap.put("patSex","男");
            else
                patientMap.put("patSex","女");
            if(patient.getPatStatus()==1)
                patientMap.put("patStatus","未就诊");
            else if(patient.getPatStatus()==2)
                patientMap.put("patStatus","未付款");
            else
                patientMap.put("patStatus","已完成");
//            String newContact=patient.getPatContact().substring(0,3)+"****"+
//                patient.getPatContact().substring(7);
//            patientMap.put("patContact",newContact);
            OfficeExample officeExample=new OfficeExample();
            OfficeExample.Criteria criteria1=officeExample.createCriteria();
            criteria1.andIdEqualTo( patient.getPatOfcId());
            System.out.println(patient.getPatOfcId());
            List<Office> offices=officeMapper.selectByExample(officeExample);
            System.out.println(offices);
            patientMap.put("patOfcName",offices.get(0).getOfcName());
            patientMap.put("patConven",patient.getPatConven());
            patientMap.put("patAge",patient.getPatAge().toString());
            String creatime=new SimpleDateFormat("yyyy-MM-dd").format( patient.getPatCreateTime());
            patientMap.put("patCreateTime",creatime);
            patientMap.put("patRemark",patient.getPatRemark());
            petientList.add(patientMap);
        }
        return JSON.toJSONString(petientList);
    }

    public Map map(Patient patient){
        Map<String,String> patientMap=new HashMap<>();
        patientMap.put("id",patient.getId().toString());
        patientMap.put("patName",patient.getPatName());
        if(patient.getPatSex()==1)
            patientMap.put("patSex","男");
        else
            patientMap.put("patSex","女");
        if(patient.getPatStatus()==1)
            patientMap.put("patStatus","未就诊");
        else if(patient.getPatStatus()==2)
            patientMap.put("patStatus","未付款");
        else
            patientMap.put("patStatus","已完成");
        OfficeExample officeExample=new OfficeExample();
        OfficeExample.Criteria criteria1=officeExample.createCriteria();
        criteria1.andIdEqualTo( patient.getPatOfcId());
        System.out.println(patient.getPatOfcId());
        List<Office> offices=officeMapper.selectByExample(officeExample);
        System.out.println(offices);
        patientMap.put("patOfcName",offices.get(0).getOfcName());
//        String newContact=patient.getPatContact().substring(0,3)+"****"+
//                patient.getPatContact().substring(7);
//        patientMap.put("patContact",newContact);
        patientMap.put("patConven",patient.getPatConven());
        patientMap.put("patAge",patient.getPatAge().toString());
//        String creatime=new SimpleDateFormat("yyyy-MM-dd").format( patient.getPatCreateTime());
//        patientMap.put("patCreateTime",creatime);
        patientMap.put("patRemark",patient.getPatRemark());
        return patientMap;
    }

    @Override
    public String pageResult(int ofcId,int currPage, int pageSize) {
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatOfcIdEqualTo(ofcId);
        System.out.println(ofcId);
        List<Patient> list=patientMapper.selectByExample(patientExample);
        for (Patient patient : list) {
            System.out.println(patient.getPatOfcId());
        }
        int size=list.size();
        System.out.println(size+"********"+list+"******");
        List<Map<String, String>> resultList = new ArrayList<>();

        int firstIndex = (currPage - 1) * pageSize;
        int lastIndex = currPage * pageSize;
        if (currPage * pageSize > size) {
            lastIndex = size;
        }
        for (Patient patient : list.subList(firstIndex,lastIndex)) {
            resultList.add(map(patient));
        }
        phyexamService.listLessRepeat(resultList);
        return recordService.layuiJsonParse(resultList,size);
    }

//    @Override
//    public String getInfoBySession(Integer ofcId, String name) {
//        PatientExample patientExample=new PatientExample();
//        PatientExample.Criteria criteria=patientExample.createCriteria();
//        criteria.andPatOfcIdEqualTo(ofcId).andPatNameEqualTo(name);
//        List<Patient> list=patientMapper.selectByExample(patientExample);
//
//        Map<String,String> patientMap;
//        List<Map<String,String>> petientList=new ArrayList<>();
//        for (Patient patient : list) {
//            patientMap=new HashMap<>();
//            patientMap.put("id",patient.getId().toString());
//            patientMap.put("patName",patient.getPatName());
//            if(patient.getPatSex()==1)
//                patientMap.put("patSex","男");
//            else
//                patientMap.put("patSex","女");
//            if(patient.getPatStatus()==1)
//                patientMap.put("patStatus","未就诊");
//            else if(patient.getPatStatus()==2)
//                patientMap.put("patStatus","未付款");
//            else
//                patientMap.put("patStatus","已完成");
////            String newContact=patient.getPatContact().substring(0,3)+"****"+
////                patient.getPatContact().substring(7);
////            patientMap.put("patContact",newContact);
//            OfficeExample officeExample=new OfficeExample();
//            OfficeExample.Criteria criteria1=officeExample.createCriteria();
//            criteria1.andIdEqualTo( patient.getPatOfcId());
//            System.out.println(patient.getPatOfcId());
//            List<Office> offices=officeMapper.selectByExample(officeExample);
//            System.out.println(offices);
//            patientMap.put("patOfcName",offices.get(0).getOfcName());
//            patientMap.put("patConven",patient.getPatConven());
//            patientMap.put("patAge",patient.getPatAge().toString());
//            String creatime=new SimpleDateFormat("yyyy-MM-dd").format( patient.getPatCreateTime());
//            patientMap.put("patCreateTime",creatime);
//            patientMap.put("patRemark",patient.getPatRemark());
//            petientList.add(patientMap);
//        }
//        return JSON.toJSONString(petientList);
//    }

//    @Override
//    public String pageResult(int ofcId, int currPage, int pageSize, String name) {
//        PatientExample patientExample=new PatientExample();
//        PatientExample.Criteria criteria=patientExample.createCriteria();
//        criteria.andPatOfcIdEqualTo(ofcId).andPatNameEqualTo(name);
//        System.out.println(ofcId);
//        List<Patient> list=patientMapper.selectByExample(patientExample);
//        for (Patient patient : list) {
//            System.out.println(patient.getPatOfcId());
//        }
//        int size=list.size();
//        System.out.println(size+"********"+list+"******");
//        List<Map<String, String>> resultList = new ArrayList<>();
//
//        int firstIndex = (currPage - 1) * pageSize;
//        int lastIndex = currPage * pageSize;
//        if (currPage * pageSize > size) {
//            lastIndex = size;
//        }
//        for (Patient patient : list.subList(firstIndex,lastIndex)) {
//            resultList.add(map(patient));
//        }
//        phyexamService.listLessRepeat(resultList);
//        return recordService.layuiJsonParse(resultList,size);
//    }

    @Override
    public String getConvenByOfcIdAndStatus(int ofcId, int Status) {
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatOfcIdEqualTo(ofcId).andPatStatusEqualTo(Status);
        List<Patient> list=patientMapper.selectByExample(patientExample);
        Map<String,String> map;
        List<Map<String,String>> list1=new ArrayList<>();
        for (Patient patient : list) {
            map=new HashMap<>();
            map.put("id",patient.getId().toString());
            map.put("patConven",patient.getPatConven());
            list1.add(map);
        }
        return JSON.toJSONString(list1);
    }

    @Override
    public String getPatientById(int id) {
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Patient> list=patientMapper.selectByExample(patientExample);
        System.out.println(list);
        Map<String,String> map=new HashMap<>();
        map.put("patConven",list.get(0).getPatConven());
        map.put("patName",list.get(0).getPatName());
        map.put("patAge",list.get(0).getPatAge().toString());
        if(list.get(0).getPatSex()==1)
        map.put("patSex","男");
        else
            map.put("patSex","女");
        map.put("patRemark",list.get(0).getPatRemark());
        OfficeExample officeExample=new OfficeExample();
        OfficeExample.Criteria criteria1=officeExample.createCriteria();
        criteria1.andIdEqualTo( list.get(0).getPatOfcId());
        System.out.println(list.get(0).getPatOfcId());
        List<Office> offices=officeMapper.selectByExample(officeExample);
        System.out.println(offices);
        map.put("patContact",list.get(0).getPatContact());
        map.put("patOfcName",offices.get(0).getOfcName());
        return JSON.toJSONString(map);
    }

    @Override
    public String getAllUserByOfcId(int id) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andOfcIdEqualTo(id);
        List<User> list=userMapper.selectByExample(userExample);
        List<Map<String,String>> list1=new ArrayList<>();
        Map<String,String> map;
        for (User user : list) {
            map=new HashMap<>();
            map.put("userId",user.getId().toString());
            map.put("userName",user.getAdName());
            list1.add(map);
        }
        return JSON.toJSONString(list1);
    }

    @Override
    public int insertOneInfo(Record record) {

        return recordMapper.insert(record);
    }

//    @Override
//    public String getAllConven(Integer ofcId) {
//        PatientExample patientExample=new PatientExample();
//        PatientExample.Criteria criteria=patientExample.createCriteria();
//        criteria.andPatOfcIdEqualTo(ofcId);
//        List<Patient> list=patientMapper.selectByExample(patientExample);
//        Map<String,String> map;
//        List<Map<String,String>> list1=new ArrayList<>();
//        for (Patient patient : list) {
//            map=new HashMap<>();
//            map.put("id",patient.getId().toString());
//            map.put("patName",patient.getPatName());
//            map.put("patStatus",patient.getPatStatus().toString());
//            map.put("patAge",patient.getPatAge().toString());
//            if(patient.getPatSex()==1)
//            map.put("patSex","男");
//            else
//                map.put("patSex","女");
//        }
//        return null;
//    }

    @Override
    public String getInfoBySession1(Integer ofcId,Integer status) {
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria=patientExample.createCriteria();
        criteria.andPatOfcIdEqualTo(ofcId).andPatStatusEqualTo(status);
        List<Patient> list=patientMapper.selectByExample(patientExample);

        Map<String,String> patientMap;
        List<Map<String,String>> petientList=new ArrayList<>();
        for (Patient patient : list) {
            patientMap=new HashMap<>();
            patientMap.put("id",patient.getId().toString());
            patientMap.put("patName",patient.getPatName());
            if(patient.getPatSex()==1)
                patientMap.put("patSex","男");
            else
                patientMap.put("patSex","女");
            if(patient.getPatStatus()==1)
                patientMap.put("patStatus","未就诊");
            else if(patient.getPatStatus()==2)
                patientMap.put("patStatus","未付款");
            else
                patientMap.put("patStatus","已完成");
//            String newContact=patient.getPatContact().substring(0,3)+"****"+
//                patient.getPatContact().substring(7);
//            patientMap.put("patContact",newContact);
            OfficeExample officeExample=new OfficeExample();
            OfficeExample.Criteria criteria1=officeExample.createCriteria();
            criteria1.andIdEqualTo( patient.getPatOfcId());
            System.out.println(patient.getPatOfcId());
            List<Office> offices=officeMapper.selectByExample(officeExample);
            System.out.println(offices);
            patientMap.put("patOfcName",offices.get(0).getOfcName());
            patientMap.put("patConven",patient.getPatConven());
            patientMap.put("patAge",patient.getPatAge().toString());
            String creatime=new SimpleDateFormat("yyyy-MM-dd").format( patient.getPatCreateTime());
            patientMap.put("patCreateTime",creatime);
            patientMap.put("patRemark",patient.getPatRemark());
            petientList.add(patientMap);
        }
        return JSON.toJSONString(petientList);
    }

    @Override
    public String getRecordByTimeAndId(String name) {
        int count=0;
        int ofcId=recordService.getOfcIdBySession(name);
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria1=patientExample.createCriteria();
        criteria1.andPatCreateTimeEqualTo(patientService.getTimeToSql()).andPatOfcIdEqualTo(ofcId);
        List<Patient> patients=patientMapper.selectByExample(patientExample);
        Map<String,String> map;
        List<Map<String,String>> list=new ArrayList<>();
        for (Patient patient : patients) {
            RecordExample recordExample=new RecordExample();
            RecordExample.Criteria criteria=recordExample.createCriteria();
            criteria.andRecPatIdEqualTo(patient.getId());
            List<Record> records= recordMapper.selectByExample(recordExample);
            for (Record record : records) {
                count++;
                UserExample userExample=new UserExample();
                UserExample.Criteria criteria2=userExample.createCriteria();
                criteria2.andIdEqualTo(record.getRecPhysicanId());
                List<User> userList=userMapper.selectByExample(userExample);
                for (User user : userList) {
                    map=new HashMap<>();
                    map.put("id",record.getId().toString());
                    map.put("userName",user.getAdName());
                    map.put("result",record.getRecResult());
                    map.put("remark",record.getRecRemark());
                    map.put("patConven",patient.getPatConven());
                    map.put("patName",patient.getPatName());
                    map.put("patCreatTime",patientService.getTime1(patient.getPatCreateTime()));
                    if(patient.getPatSex()==1)
                        map.put("patSex","男");
                    else
                        map.put("patSex","女");
                    map.put("patAge",patient.getPatAge().toString());
                    map.put("patRemark",patient.getPatRemark());
                    map.put("patContact",patient.getPatContact());
                    OfficeExample officeExample=new OfficeExample();
                    OfficeExample.Criteria criteria3=officeExample.createCriteria();
                    criteria3.andIdEqualTo(patient.getPatOfcId());
                    List<Office> offices=officeMapper.selectByExample(officeExample);
                    map.put("patOfcName",offices.get(0).getOfcName());
                    map.put("count",Integer.toString(count));
                    list.add(map);
                }

            }
        }
        return JSON.toJSONString(list);
    }

    @Override
    public String getAllRecord(String name) {
        int count=0;
        int ofcId=recordService.getOfcIdBySession(name);
        PatientExample patientExample=new PatientExample();
        PatientExample.Criteria criteria1=patientExample.createCriteria();
        criteria1.andPatOfcIdEqualTo(ofcId);

        List<Patient> patients=patientMapper.selectByExample(patientExample);
        System.out.println(patients.size()+"获取病人的长度");

        Map<String,String> map;
        List<Map<String,String>> list=new ArrayList<>();
        for (Patient patient : patients) {
            RecordExample recordExample=new RecordExample();
            RecordExample.Criteria criteria=recordExample.createCriteria();
            criteria.andRecPatIdEqualTo(patient.getId());
            List<Record> records= recordMapper.selectByExample(recordExample);
            for (Record record : records) {
                count++;
                UserExample userExample=new UserExample();
                UserExample.Criteria criteria2=userExample.createCriteria();
                criteria2.andIdEqualTo(record.getRecPhysicanId());
                List<User> userList=userMapper.selectByExample(userExample);
                for (User user : userList) {
                    map=new HashMap<>();
                    map.put("id",record.getId().toString());
                    map.put("userName",user.getAdName());
                    map.put("result",record.getRecResult());
                    map.put("remark",record.getRecRemark());
                    map.put("patConven",patient.getPatConven());
                    map.put("patName",patient.getPatName());
                    map.put("patCreatTime",patientService.getTime1(patient.getPatCreateTime()));
                    if(patient.getPatSex()==1)
                        map.put("patSex","男");
                    else
                        map.put("patSex","女");
                    map.put("patAge",patient.getPatAge().toString());
                    map.put("patRemark",patient.getPatRemark());
                    map.put("patContact",patient.getPatContact());
                    OfficeExample officeExample=new OfficeExample();
                    OfficeExample.Criteria criteria3=officeExample.createCriteria();
                    criteria3.andIdEqualTo(patient.getPatOfcId());
                    List<Office> offices=officeMapper.selectByExample(officeExample);
                    map.put("patOfcName",offices.get(0).getOfcName());
                    map.put("count",Integer.toString(count));
                    list.add(map);
                }

            }
        }
        System.out.println(list);
        return JSON.toJSONString(list);
    }

    @Override
    public int getUserIdBySession(String name) {
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andAdNameEqualTo(name);
        List<User> list= userMapper.selectByExample(userExample);
        int id=list.get(0).getId();
        System.out.println(id+"***id****");
        return  id;
    }

    @Override
    public int deleteOne(Integer id) {
        RecordExample recordExample=new RecordExample();
        RecordExample.Criteria criteria=recordExample.createCriteria();
        criteria.andIdEqualTo(id);

        return recordMapper.deleteByExample(recordExample);
    }
}
